package cn.com.interceptor;

import cn.com.beans.AccountInfo;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


@Component
public class SysInterceptor implements HandlerInterceptor {
    /**
     * 进入拦截器后首先进入的方法
     * 返回false则不再继续执行
     * 返回true则继续执行
     */

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                             Object handler)throws Exception {
        System.out.println("进拦截器了");
        Object account = request.getSession().getAttribute("account");

        if(account != null) {//已登录
            System.out.println("用户已登录");
            System.out.println("account是 = " + account);
            return true;
        }else {//未登录
            //直接重定向到登录页面
            System.out.println("用户未登陆");
            response.sendRedirect("/user/login");
            return false;
        }
    }

}